var getStrCharLen = (val) => {
	var str = new String(val);
	var bytesCount = 0;
	for (var i = 0, n = str.length; i < n; i++) {
		var c = str.charCodeAt(i);
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			bytesCount += 1;
		} else {
			bytesCount += 2;
		}
	}
	return bytesCount;
}

var repeat = (str, n) => {
	return new Array(n + 1).join(str);
}

var escape2Html = (str) => {
	if (!str) return '';
	return str.replace(/<image/gi, '<image style="max-width: 100%;height: auto;" ')
		.replace(/<section/g, '<div')
		.replace(/<article/g, '<div')
		.replace(/\/section>/g, '/div>')
		.replace(/\/article>/g, '/div>')
}

var formatTime = (number, format = 'Y年M月D日 h:m:s') => {
	var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	var returnArr = [];

	var date = new Date(number * 1000);
	returnArr.push(date.getFullYear());
	returnArr.push(formatNumber(date.getMonth() + 1));
	returnArr.push(formatNumber(date.getDate()));

	returnArr.push(formatNumber(date.getHours()));
	returnArr.push(formatNumber(date.getMinutes()));
	returnArr.push(formatNumber(date.getSeconds()));

	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}
var formatDateTime = (date = new Date()) => {
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');

	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

var formatNumber = (n) => {
	n = n.toString()
	return n[1] ? n : '0' + n
}
// 解析URL 参数
function changeChinese(val) {
	var str = null
	if (val == 1) {
		str = '一'
	} else if (val == 2) {
		str = '二'
	} else if (val == 3) {
		str = '三'
	} else if (val == 4) {
		str = '四'
	} else if (val == 5) {
		str = '五'
	}
	return str

}
/* 
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html) {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
	return newContent;
}
// 预览图片
function previewImage(url, index) {
	uni.previewImage({
		current: index, // 当前显示图片的链接/索引值
		urls: [url], // 需要预览的图片链接列表，photoList要求必须是数组
		loop: true // 是否可循环预览
	});
}

function timestampFormat(timestamp) {
	function zeroize(num) {
		return (String(num).length == 1 ? '0' : '') + num;
	}
	var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
	var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
	var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
	var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
	var Y = tmDate.getFullYear(),
		m = tmDate.getMonth() + 1,
		d = tmDate.getDate();
	var H = tmDate.getHours(),
		i = tmDate.getMinutes(),
		s = tmDate.getSeconds();
	if (timestampDiff < 60) { // 一分钟以内
		return "刚刚";
	} else if (timestampDiff < 3600) { // 一小时前之内
		return Math.floor(timestampDiff / 60) + "分钟前";
	} else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
		return '今天' + zeroize(H) + ':' + zeroize(i);
	} else {
		var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
		if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
			return '昨天' + zeroize(H) + ':' + zeroize(i);
		} else if (curDate.getFullYear() == Y) {
			return zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
		} else {
			return Y + '年' + zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
		}
	}
}
//参数的时间不能大于当前时间，大于当前时间会返回“刚刚”。
// //用法展示：
// timestampFormat(1326170770); //2012年01月10日 12:46
// timestampFormat(Date.parse('2016-10-11 15:26:10')/1000); //刚刚
// timestampFormat(Date.parse('2016-10-11 15:10:10')/1000); //16分钟前
// timestampFormat(Date.parse('2016-10-11 10:10:10')/1000); //今天10:10
// timestampFormat(Date.parse('2016-10-10 10:10:10')/1000); //昨天10:10
// timestampFormat(Date.parse('2016-02-10 10:10:10')/1000); //02月10日 10:10
// timestampFormat(Date.parse('2012-10-10 10:10:10')/1000); //2012年10月10日 10:10
function imgfileSuffix(fileSuffix) {
	return fileSuffix == 'png' || fileSuffix == 'jpg' || fileSuffix == 'jpge'
}
export default {
	previewImage,
	getStrCharLen,
	repeat,
	escape2Html,
	formatTime,
	changeChinese,
	formatRichText,
	timestampFormat,
	imgfileSuffix,
	formatDateTime
}